/**
 * 
 */
package com.okaram.ChildrenBooks;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * @author curri
 * This class represents reading a book
 */
public class BookReadActivity extends Activity {
	int currentPage;
	ImageView iv;
	TextView tv;
	List<Page> pages;
/*	Page[] ps={ 
			new Page("Alphabet/apple.jpg", "Alphabet/A.ogg","A is for Apple"),
			new Page("Alphabet/C.jpg", "Alphabet/C.ogg","C is for Cat"),
			new Page("Alphabet/D.jpg", "Alphabet/D.ogg","D is for Duck"),		
	};
	*/
	private static class Page {
		public String image, sound, text;
		public Page(String base,String i, String s, String t) {
			image=base+i;
			sound=base+s;
			text=t;
		}
	}

	
    @Override
    public void onCreate(Bundle b) {
    	
        super.onCreate(b);
        setContentView(R.layout.read);
        setTitle("Alphabet book");
        iv=(ImageView)findViewById(R.id.imageView1);
        tv=(TextView)findViewById(R.id.text);
        String path=getIntent().getStringExtra("path");
        if(path!=null) {
            pages=readBook(path);            
        }
        currentPage=0;
        if(b!=null) {
        		int cp=b.getInt("currentPage",-1);
        		if(cp!=-1)
        			currentPage= cp;
        }
        setPage(pages.get(currentPage));
    }
/*	
    @Override public void onRestoreInstanceState(Bundle b) {
    		int cp=b.getInt("currentPage",-1);
    		if(cp!=-1) {
    			currentPage=cp;
    			setPage(pages.get(currentPage));
    		}
    }
*/    
    @Override public void onSaveInstanceState(Bundle b) {
    		b.putInt("currentPage", currentPage);
    }
	
    List<Page> readBook(String base) {
    		ArrayList<Page> pages=new ArrayList<Page>();
    		BufferedReader in=null;
    		try {
			in=new BufferedReader( new InputStreamReader(getAssets().open(base+"book.txt")));
    			while(true) {
    				String img=in.readLine(), snd=in.readLine(), txt=in.readLine();
    				if(img==null || snd==null || txt==null)
    					break;
    				pages.add(new Page(base,img,snd,txt));
    			}
    		} catch (IOException e) {
    			
    		}
    		finally{ 
    			try {in.close();} catch(IOException e){}}
    		return pages;
    }
	private void setPage(Page p){
        try {
    			iv.setImageBitmap(getBitmapFromAsset(p.image));
    			tv.setText(p.text);
    			playSound(p.sound);
        } catch(Exception e) {
    			Log.d("load", e.toString());
        }
	}	
    
    private void playSound(String sound) throws IOException {
		MediaPlayer mp=new MediaPlayer();
		AssetFileDescriptor snd=getAssets().openFd(sound);
		mp.setDataSource(snd.getFileDescriptor(),snd.getStartOffset(),snd.getLength());
		mp.prepare();
		mp.start();    	
    }
    /**
     * Helper Function - from stackOverflow
     * @throws IOException 
     */
    private Bitmap getBitmapFromAsset(String strName) throws IOException
    {
        AssetManager assetManager = getAssets();

        InputStream istr = assetManager.open(strName);
        Bitmap bitmap = BitmapFactory.decodeStream(istr);

        return bitmap;
    }

    public void prevPage(View v) {
    		if(currentPage>0) {
    			--currentPage;
    			setPage(pages.get(currentPage));
    		}
    }

    public void nextPage(View v) {
    		if(currentPage<pages.size()-1) {
    			++currentPage;
    			setPage(pages.get(currentPage));    			
    		}
    }
    
    public void playAgain(View v) {
    		try {
    			playSound(pages.get(currentPage).sound);
    		} catch (IOException e) {}
    }
    

}
